﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="App.xaml.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Interaction logic for App.xaml
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Windows.Client
{
    using System;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Markup;
    using Business.Mapping;
    using Common;
    using Hisaab.Common;

    using Microsoft.Practices.ServiceLocation;

    using ViewModel;

    /// <summary>
    /// Defines the <seealso cref="App"/> type.
    /// </summary>
    public partial class App
    {
        /// <summary>
        /// Raises the <see cref="E:System.Windows.Application.Startup"/> event.
        /// </summary>
        /// <param name="e">A <see cref="T:System.Windows.StartupEventArgs"/> that contains the event data.</param>
        protected override void OnStartup(StartupEventArgs e)
        {
            try
            {
                FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
                ObjectMapper.AddProfile(new BusinessMapping());
                ObjectMapper.AddProfile(new ClientMapping());

                /*var session = new SessionManager();
                ServiceLocator.Current.SetInjection<ISession>(session);*/

                if (Hisaab.Windows.Client.Properties.Settings.Default.ShowLogin)
                {
                    ShowLogin();
                }
                else
                {
                    ShowMainWindow();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format(Hisaab.Windows.Client.Properties.Resources.ApplicationStartErrorMessage, ex.Message), Hisaab.Windows.Client.Properties.Resources.ApplicationName);
            }
        }

        /// <summary>
        /// Shows the login.
        /// </summary>
        private static void ShowLogin()
        {
            var loginViewModel = new LoginViewModel(ShowMainWindow);
            ServiceLocator.Current.GetInstance<IWindowDialogService>().ShowDialog("Login", loginViewModel);
        }

        /// <summary>
        /// Shows the main window.
        /// </summary>
        private static void ShowMainWindow()
        {
            var mainWindow = CreateMainWindow();

            new DocumentManagerAdapter(mainWindow.MasterDocumentContainer, mainWindow.RibbonControl);

            mainWindow.Show();
        }

        /// <summary>
        /// Creates the main window.
        /// </summary>
        /// <returns>Main window</returns>
        private static IMainWindow CreateMainWindow()
        {
            if (Hisaab.Windows.Client.Properties.Settings.Default.UseFluentUI)
            {
                return new FluentMainWindow
                {
                    DataContext = new MainWindowViewModel()
                };
            }

            return new RibbonMainWindow
                       {
                           DataContext = new MainWindowViewModel()
                       };
        }
    }
}